<?php 
$path = drupal_get_path('module', 'forms');
drupal_add_css($path . '/css/forms.css');

function formshome(){ /* Function to list departments associated with forms*/
global $base_url;
global $language;
	$url = $base_url . '/' . drupal_get_path('module', 'forms');
	 if($language->language=='ta'){
		$lang= 'ta';
	}else{
		$lang='';
	}
	 $output = '<div class="rsslogo">' . get_rss_feed("forms_rssfeed") . '</div>	';

	$output .= get_forms_header('dept');
	my_db_change(1);
	$result = db_query("SELECT DISTINCT(c.dep_id), c.dept_name  FROM
				node AS a, field_data_field_form_dept AS b, depart_mst AS c
				WHERE a.nid = b.entity_id  AND a.status = 1 AND (a.language='$language->language' or a.language = '')
				AND c.dep_id = CAST(b.field_form_dept_value AS INTEGER) 
				ORDER BY c.dept_name");
	$op = "<div class='form_head_list'>";
	$st =1;
	foreach ($result as $dept) {
	 	if($st%2!=0) { 
			$cls = "first_clm";
		} 
		else {
			$cls = "second_clm";
		}
	
		 $op .= "<div class='".$cls." d".$dept->dep_id."'>";
		 $op .= "<img src='".$url."/images/icon_main.png' alt='More' /> "; 
		 $op .= l(trim(t($dept->dept_name)),$lang.'/forms/deptname/'.$dept->dep_id)."</div>";	 
		 
		 $st++;

	}
	$op .="</div>";
	$output .= "<div class='result_inner'>";
	$output .= $op;
	$output .= "<div class='clr'></div></div>";	
	my_db_change(0);
return $output;
}

/**
 * Function for forms_dept_detail()
 * @return Ambigous <An, string>
 */
function forms_dept_detail(){
	global $base_url;
	global $language;
	$depid = arg(2);

	$output .= '<div class="rsslogo">' . get_rss_feed("forms_rssfeed/$depid") . '</div>	';

	$url = $base_url . '/' . drupal_get_path('module', 'forms');
	$output .= get_forms_header('dept');
 	 if($language->language=='ta'){
		$lang= '/ta';
	}else{
		$lang='';
	}
	my_db_change(1); 
	
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'addnewform')   
	  ->fieldCondition('field_form_dept', 'value', $depid, '=')
	  ->propertyOrderBy('title','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, ''), 'IN')
	   ->pager(15,0);
	  
	$result = $query->execute();	
		
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 
	  
	  $news_items  = node_load_multiple($news_items_nids, array('type' => 'addnewform'));
	}
	//print "<pre>";print_r($news_items);print "</pre>";
	my_db_change(0);

	$output .= theme('forms_dep_profile_theme',array('items' => $news_items_nids,'depid'=>$depid,'nid'=>$nid));
	$output .= theme('pager');
	// $output .= '</div>';
return $output;
}

/**
 * Function formscategoryhome()
 * @return string
 */
function formscategoryhome(){
global $base_url;
global $language;
	$url = $base_url . '/' . drupal_get_path('module', 'forms');
	 if($language->language=='ta'){
		$lang= 'ta';
	}else{
		$lang='';
	}
	$output = get_forms_header('cate');
	my_db_change(1);
	$sql = db_query("select * from category_mst where cate_id in (select distinct(cast(field_form_category_value as integer))
 as cateidvalue from field_data_field_form_category a,node n where a.entity_id=n.nid  and n.vid=a.revision_id and n.status=1 and
  (n.language='".$language->language."' or n.language = '') ) order by cate_id");
	
	my_db_change(0);
	$op = "<div class='form_head_list'>";
	$st =1;
	foreach ($sql as $dept) {
	 	if($st%2!=0) { 
			$cls = "first_clm";
		} 
		else {
			$cls = "second_clm";
		}
	
		 $op .= "<div class='".$cls." d".$dept->cate_id."'>";
		 $op .= "<img src='".$url."/images/icon_main.png' alt='More' /> "; 
		 $op .= l(trim(t($dept->cate_name)),$lang.'/forms/category/'.$dept->cate_id)."</div>";	 
		 
		 $st++;

	}
	$op .="</div>";
	$output .= "<div class='result_inner'>";
	$output .= $op;
	$output .= "<div class='clr'></div></div>";	
return $output;
}

/**
 * Function formscatewise()
 * @return Ambigous <An, string>
 */
function formscatewise(){
global $base_url;
global $language;
	$url = $base_url . '/' . drupal_get_path('module', 'forms');
	 if($language->language=='ta'){
		$lang= '/ta';
	}else{
		$lang='';
	}
	$output = get_forms_header('cate');
	my_db_change(1); 
	$catid = arg(2);

	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'addnewform')   
	  ->fieldCondition('field_form_category', 'value', $catid, '=')
	  ->propertyOrderBy('title','ASC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, ''), 'IN')
	   ->pager(25,0);
	  
	$result = $query->execute();	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 
	  
	  $news_items  = node_load_multiple($news_items_nids, array('type' => 'addnewform'));
	}
	//print "<pre>";print_r($news_items);print "</pre>";
	my_db_change(0);
	$output .= '<div class="result_inner">';
	$output .='<div class="res_dept"><span><strong>'.t(category_form_name($catid)).'</strong></span>';
	$output .='<span class="backbut"><a href="'.$base_url.$lang.'/forms/deptname">'.t('Back').'</a></span></div>';
	$output .= theme('forms_category_theme',array('items' => $news_items_nids,'catid'=>$catid,'nid'=>$nid));
	$output .='<div class = "clear"></div></div>';
	$output .= theme('pager'); 
return $output;
}

/**
 * Function formsalpha()
 * @return Ambigous <An, string>
 */
function formsalpha(){
global $base_url;
global $language;
$alp = arg(2);
	$url = $base_url . '/' . drupal_get_path('module', 'forms');
	
	$output = get_forms_header('atoz');
	$output .= "</div><div class='view-header'>" . AtoZ_letters($alp) . "</div>";
	$output .= "<div class='result_inner'>";
	$val= strtoupper($alp);	
	 
	 my_db_change(1);
		if($val != 'ALL'){
 
			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'addnewform')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$val.'%','LIKE')
			  ->propertyCondition('language', array($GLOBALS['language']->language, ''), 'IN')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 
		 }
		 else {
 
			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'addnewform')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('language', array($GLOBALS['language']->language, ''), 'IN')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 
		 }
		$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 	  
	  $items  = node_load_multiple($news_items_nids, array('type' => 'addnewform'));
	}
	//print "<pre>";print_r($items);print "</pre>";
	my_db_change(0);
	$output .= theme('forms_category_theme',array('items' => $news_items_nids,'nid'=>$nid));
	$output .= theme('pager');
return $output;	
}

/**
 * Function for forms_search_form()
 * @return string
 */
function forms_search_form(){
global $language,$base_url;
	$url = $base_url . '/' . drupal_get_path('module', 'form');
	$output = get_forms_header('find');
	$output .= '<div class="result_inner_search">'. drupal_render(drupal_get_form('forms_search'));

return $output;
}

/**
 * Function for forms_search()
 * @param unknown_type $form_state
 * @return multitype:string multitype:string  Ambigous <The, string, A, Optional>
 */
function forms_search($form_state){	
	$keyword = trim(arg(2));
	$form['forms_search'] = array(
	'#type' => 'textfield',
	'#title' => t('Keyword'),
	'#default_value' => $keyword ? $keyword : '',
	'#size'=>'35',
	'#description'=>'(Search by title Eg: Community Certificate.)',
	);    
	
	$form[ 'field1'] = array(
		'#prefix'=>'<div class="description"> (Search by title Eg: Community Certificate.) </div>'
	);
	
    $form[ 'forms_submit' ] = array(
	'#title' => 'Submit', 
	'#type' => 'submit', 
	'#value' => 'Go', 
	'#name' => 'search',
	'#prefix' => '<div class="sea_go_but">',
	'#suffix' => '</div>',
	
	);
	
	return $form;
	
}

function forms_search_submit($form,&$form_state){
	global $base_url;	
	
	//print "form submit";  
	 
	 //print "<pre>"; print_r($form_state); print "</pre>";
	 //drupal_set_message(t('The form has been submitted.')); die;
	 
	 	if ($form_state[clicked_button]['#name'] == 'search') {		
			$str = $form_state[values][forms_search];
			if(empty($str)) {
			form_set_error('forms_search','Please Enter Key Word');
			echo 'Please Enter Key Word';
			}
			
		}
		
				
	$form_state['redirect'] = 'forms/search/'.$str; 
    return;
}


function forms_search_result_form() {
global $language,$base_url;
$url = $base_url . '/' . drupal_get_path('module', 'forms');
$output = forms_search_form();
$go_keyword= trim(arg(2));
		$go_keyword= strtolower($go_keyword);	
		//$ser_string = array();
		$go_keyword = preg_replace('/[\p{Z}\s]{2,}/u', ' ', $go_keyword);
		$go_keyword=pg_escape_string($go_keyword); 
		
		$len = drupal_strlen($go_keyword);
		$keyword1 =$go_keyword; 
		
		
			// Do not count UTF-8 continuation bytes.
		 if(!empty($keyword1))
		 {
		$position[0]=strpos($keyword1,'"');
		
		 if($position[0] != "");{
		
		  $position[1]=strpos($keyword1,'"',$position[0]+1);
		
		  }
		  }
		 if(!empty($position[1]))
		 {
			//echo substr($keyword,1)."</br>";
			$k=0;
			for($i=0;$i<=$len;$i++)
			{
			
			$position[0]=strpos($keyword1,'"');
			
			if(strlen($keyword1) !=0)
			{
			if($position[0] !=0 && $position[0] !="")
			$str_not_quote[$k++] =substr($keyword1,0,$position[0]);
			if($position[0] ==false)
			$str_not_quote[$k++] =substr($keyword1,0,strlen($keyword1));
			}
			
			
			 $position[1]=strpos($keyword1,'"',$position[0]+1);
			
			 if(empty($position[1]))
			 {
			 break;
			 }
			 $sub_string_len = $position[1] - $position[0]-1;
			 
		 $ser_string[$i] = substr($keyword1,$position[0]+1, $sub_string_len);
			
			 $part_string= substr($keyword1,$position[1]+1, $len);
			
			$keyword1 =trim($part_string);
			if(empty($keyword1))
			{
			break;
			}
			}
			}
			$ser_string = array_filter_customize($ser_string);
			//print_r($ser_string);
			if(!empty($ser_string))
			{
			
				$sql_search_clause = search_keyword('LOWER(n.title)', $ser_string, $str_not_quote);
			}

	$go_val = $go_keyword; 	

	my_db_change(1);
	$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'addnewform')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$go_val.'%','LIKE')
			  //->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 
	$result = $query->execute();	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 	  
	   $items  = node_load_multiple($news_items_nids, array('type' => 'addnewform'));
	}
	$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'addnewform')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$go_val.'%','LIKE')
			 // ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC'); 
	$count = $query->count()->execute();
	//print "<pre>";print_r($items);print "</pre>";
	my_db_change(0);
	if($count != '0') {
		$output .='<div class="clr"><div class="record_found">'.$count.' Records Found</div></div>';
	}
	$output .= theme('forms_category_theme',array('items' => $news_items_nids,'nid'=>$nid));
	$output .= '</div>';
	$output .= theme('pager');	
return $output;

}
function get_forms_header($css){
	global $base_url;
	 if($css=='cate') { $cate_cls= "view_head_sel"; }else {$cate_cls='view_head_nonsel'; }
     if($css=='dept') { $dept_cls= "view_head_sel"; }else {$dept_cls='view_head_nonsel'; }
     if($css=='find') { $find_cls= "view_head_sel"; }else {$find_cls='view_head_nonsel'; }
	 if($css=='atoz') { $atoz_cls= "view_head_sel"; }else {$atoz_cls='view_head_nonsel'; }
	  
	   
	global $language;
	if($language->language=='ta'){
		$css='addnewform_ta';
		$lang ='/ta';
		
	}
	else{
		$css='addnewform';
		$lang = '';
	}
	
	$html = "<div class='view-header' id='viewmod_$css'>
				<div class='inner_tab_content'>
				<div class='inner_li'><a class='$dept_cls' href='$base_url$lang/forms/deptname'>".t('Department wise')."</a></div>
				<div class='inner_li'><a class='$cate_cls' href='$base_url$lang/forms/category'>".t('Category Wise')."</a></div>
				<div class='inner_li'><a class='$atoz_cls' href='$base_url$lang/forms/atoz'>".t('A to Z')."</a></div>
				<div class='inner_li'><a class='$find_cls' href='$base_url$lang/forms/search'>".t('Search')."</a></div>
				<div class='clr'></div></div>
			</div>";
	return $html;
}
function AtoZ_letters($alp){
	global $base_url;
	
		//print_r( strtolower($alpha ));
	for($i =65; $i < 91; $i++){
			$class = "";
	
// strtolower($val );
		if(chr($i) == $alp){
			$class = " view_head_sel";
		}
		else
		{
			$class = " ";
		}
		//$linkOp .= l($val,'forms/atoz/'.$val,array('attributes'=>array('class'=>$class)))."&nbsp;&nbsp;";
		 $linkOp .= '<a class = "myclass' . $class . '"  href = "' .$base_url.'/forms/atoz/'. chr($i) .'" >' . chr($i) . '</a>';
	}
	$class = "";
	if($alp == 'All' || $alp == ''){
		$class = " view_head_sel";
	}
	$linkOp .= '<a class = "myclass' . $class . '" href = "' .$base_url.'/forms/atoz/All" title = "All" id = "All"> All </a>';
	$StrOp = "<div class='result_inner_atoz'>";
	$StrEnd = "</div>";
	$Grey = "<div class='grey_bord_line'></div>";//$linkOp .="<br /><br />";
	return $StrOp . $linkOp . $Grey . $StrEnd;
}
?>
